//
//  ColorCell.swift
//  demo01
//
//  Created by babybus on 2025/6/26.
//

import Foundation	
import UIKit
import SnapKit

//struct colorBlock{
//    var nameOfColor:String
//    var typeOfColor:UIColor
//    var showBorder:Bool
//}
class ColorCell:UICollectionViewCell{
    
    
    public var showBorder:Bool = {return false}()
    public let colorButton:UIButton={
        let button = UIButton();
        button.tag = 0
        button.isEnabled = false
        return button
    }()
    private let colorLabel:UILabel={
        let label = UILabel();
        return label
    }()
    
    private let containerStack:UIStackView = {
        let stack = UIStackView();
        stack.axis = .horizontal
        stack.alignment = .center
        stack.distribution = .fill
        stack.spacing = 8
        return stack
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:NSCoder) -- fatalError")
    }
    func configure(colorname:String,colortype:UIColor,showBorder:Bool){
        colorLabel.text = colorname
        colorButton.backgroundColor = colortype
        self.showBorder = showBorder
    }
    
    func setupUI(){
        containerStack.addArrangedSubview(colorButton)
        containerStack.addArrangedSubview(colorLabel)

        
        contentView.addSubview(containerStack)
        
        containerStack.snp.makeConstraints{ make in
            make.left.right.equalToSuperview().inset(8)
            make.top.bottom.equalToSuperview().inset(8)
        }
        colorButton.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
    }
    override func prepareForReuse() {
        super.prepareForReuse()
    }
    @objc func didTapButton(){
        setColorButtonBorder()
        print("颜色：" + colorLabel.text!)
    }
    public func didTapCell(){
        setColorButtonBorder()
        print("颜色：" + colorLabel.text!)
    }
    
    
    public func getColorName()->String{
        return colorLabel.text!
    }
    public func setColorName(colorName:String){
        colorLabel.text = colorName
    }
    public func setColorButtonBorder(){
        if showBorder{
            colorButton.layer.borderWidth = 0
            showBorder = false
        }else{
            colorButton.layer.borderWidth = 5
            showBorder = true
        }
    }
}
